package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CarColor {
	 ResultSet rs = null;
	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 
	 
	 private static final String TABLE_Colors = "_Colors";
	 Context context;
	 public Context getContext() {
		return context;
	 }

	 public void setContext(Context context) {
		this.context = context;
	 }
	public ResultSet selectColors() throws Exception {
		 Connect con = new Connect(); 
		String StrSql   = "SELECT	 [OID]," +
	
									"[IsActive]," +
									"[IsDeleted]," +
									
									"[ColorCode],"+
									"[ColorName] " +		
                			 
                		  " FROM		 [dbo].[Colors] " +
                		  
                		  " WHERE	 [IsDeleted]='0' "; 
		
		
//		StrSql += "ORDER BY [ColorName] ASC ";
		
		return con.selectData(StrSql);
	 }
	
//	 public void closeConnecttion() throws Exception {
//    	 if (conn != null) {
//             conn.close();
//         }
//    }
	 
	 public void DEFINE_CarColor() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();
			
			ContentValues Val = new ContentValues();
			Val.put("ColorsOID", "0");
			Val.put("ColorCode", "0");
			Val.put("ColorName", "ไม่ระบุ");

			db.insert(TABLE_Colors, null, Val);
			try {
				rs = selectColors();
				while (rs.next()) {
					Val = new ContentValues();
					Val.put("ColorsOID", rs.getString("OID"));
					Val.put("ColorCode", rs.getString("ColorCode"));
					Val.put("ColorName", rs.getString("ColorName"));

					db.insert(TABLE_Colors, null, Val);
				}
				rs.close();
			} catch (Exception e) {
				Log.d("TABLE_Colors", "Create Table not Successfully.");
			}
			Log.d("TABLE_Colors", "Create Table Successfully.");
			db.close();
		}
	 
	 public String[][] SelectAll_Colors() throws Exception {
			// TODO Auto-generated method stub
			try {
				String arrData[][] = null;
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				String strSQL = "SELECT  ColorsOID As _id , * FROM " + TABLE_Colors;

				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();
				return arrData;
			} catch (Exception e) {
				return null;
			}
		}

	 
	 
}
